package Engine.UIControl
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/*
	可拖动GUI
	*/
	
	public class DragGUI
	{
		private var _bounds:Rectangle = null;   //拖动GUI约束矩阵
		
		private var _content:Sprite;
		
		private var _offsetX:Number;  
		private var _offsetY:Number; 
		
		private var DragY:Boolean = true;
		private var DragX:Boolean = false;
		
		private var MaxX:Number = 1200;
		private var MinX:Number = -100;
		
		private var MaxY:Number = 1000;
		private var MinY:Number = -100;
		
		private var updateXFun:Function = new Function();
		private var updataYFun:Function = new Function();
		
		public function DragGUI(p_content:Sprite)
		{
			this._content = p_content;
			this._content.buttonMode = true;
			this._content.addEventListener(MouseEvent.MOUSE_DOWN,onDragStrat);
		}
		
		private function onDragStrat(event:MouseEvent):void
		{
			if(this.DragX == false && this.DragY == false)
				return;
			this._offsetX = event.localX;
			this._offsetY = event.localY;
			this._content.startDrag(false,this._bounds);
			this._content.stage.addEventListener(MouseEvent.MOUSE_UP,onDragStop);
			this._content.stage.addEventListener(MouseEvent.MOUSE_MOVE,onDraging);
		}
		
		private function onDragStop(event:MouseEvent):void
		{
			this._content.stopDrag();
			this._content.stage.removeEventListener(MouseEvent.MOUSE_UP,onDragStop);
			this._content.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onDraging);
		}
		
		private function onDraging(event:MouseEvent):void
		{
			if(this.DragX)
			{
				this.updateXFun(this._content.x - this.MinX);
			}
			
			if(this.DragY)
			{
				this.updataYFun(this._content.y - this.MinY);
			}
		}
		
		public function setDrag(dragx:Boolean,dragy:Boolean):void
		{
			this.DragX = dragx;
			this.DragY = dragy;
		}
		
		/*设置最大拖动高度*/
		public function setMinMaxY(miny:Number,maxy:Number):void
		{
			this.MaxY = maxy - this._content.height; 
			this.MinY = miny;
			this._bounds = new Rectangle(this._content.x,this.MinY,0,this.MaxY - this.MinY);
			this._content.y = this.MinY;
			this.setDrag(false,true);
		}
		/*设置最大拖动宽度*/
		public function setMinMaxX(minx:Number,maxx:Number):void
		{
			this.MaxX = maxx - this._content.width;
			this.MinX = minx;
			this._bounds = new Rectangle(this.MinX,this._content.y,this.MaxX - this.MinX,0);
			this._content.x = this.MinX;
			this.setDrag(true,false);
		}
		
		/*上下拖动回调函数*/
		public function setUpdateYFun(fun:Function):void
		{
			this.updataYFun = fun;
		}
		
		//左右拖动回调函数
		public function setUpdateXFun(fun:Function):void
		{
			this.updateXFun = fun;
		}
		
		//返回可拖动高度
		public function get dragHeight():Number
		{
			return this._bounds.height;
		}
		
		//返回可拖动宽度
		public function get dragWidth():Number
		{
			return this._bounds.width;
		}
		
		public function set x(value:Number):void
		{
			var posx:Number = this.MinX + value;
			if(posx > this.MaxX)
				posx = this.MaxX;
			this._content.x = posx;
		}
		
		public function set y(value:Number):void
		{
			var posy:Number = this.MinY + value;
			if(posy > this.MaxY)
				posy = this.MaxY;
			this._content.y = posy;
		}
		
	}
}